package com.jeeplus.modules.api.utils;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.alibaba.fastjson.serializer.SerializeConfig;
import com.alibaba.fastjson.serializer.SerializerFeature;
import lombok.extern.slf4j.Slf4j;

import javax.xml.crypto.dsig.keyinfo.KeyValue;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author ：chao.wang
 * @date ：Created in 2019-08-21 16:34
 * @description：
 * @modified By：
 * @version: $
 */
@Slf4j
public class JsonUtils {

    private static final SerializeConfig config;
    private static String dateFormat;
    static {
        /*dateFormat = "yyyy-MM-dd HH:mm:ss";*/
        config = new SerializeConfig();
        /*config.put(Date.class,new SimpleDateFormatSerializer(dateFormat));*/
        config.put(Date.class, new MyDateFormatDeserializer());
//        config.put(java.util.Date.class, new JSONLibDataFormatSerializer()); // 使用和json-lib兼容的日期输出格式
//        config.put(java.sql.Date.class, new JSONLibDataFormatSerializer()); // 使用和json-lib兼容的日期输出格式
    }

    private static final SerializerFeature[] features = {SerializerFeature.WriteMapNullValue, // 输出空置字段
            SerializerFeature.WriteNullListAsEmpty, // list字段如果为null，输出为[]，而不是null
            //SerializerFeature.WriteNullNumberAsZero, // 数值字段如果为null，输出为0，而不是null
            SerializerFeature.WriteNullBooleanAsFalse, // Boolean字段如果为null，输出为false，而不是null
            SerializerFeature.WriteNullStringAsEmpty ,// 字符类型字段如果为null，输出为""，而不是null
            SerializerFeature.WriteDateUseDateFormat
    };

    // 转换为数组
    public static <T> Object[] toArray(String text) {
        return toArray(text, null);
    }

    // 转换为数组
    public static <T> Object[] toArray(String text, Class<T> clazz) {
        return JSON.parseArray(text, clazz).toArray();
    }

    // 转换为List
    public static <T> List<T> toList(String text, Class<T> clazz) {
        return JSON.parseArray(text, clazz);
    }

    public static Object beanToJson(KeyValue keyvalue) {
        String textJson = JSON.toJSONString(keyvalue);
        Object objectJson = JSON.parse(textJson);
        return objectJson;
    }

    public static Object textToJson(String text) {
        Object objectJson = JSON.parse(text);
        return objectJson;
    }


    public static String toJSONString(Object object) {
        return JSON.toJSONString(object, config, features);
    }

    public static String toJSONNoFeatures(Object object) {
        return JSON.toJSONString(object, config);
    }


    public static Object toBean(String text) {
        return JSON.parse(text);
    }

    public static <T> T toBean(String text, Class<T> clazz) {
        return JSON.parseObject(text, clazz);
    }

    public static <T> T beanToBean(Object object,Class<T> clazz){
        return  JsonUtils.toBean(JsonUtils.toJSONString(object),clazz);
    }

    public static <T> List<T> beanlistToBeanList(Object object,Class<T> clazz){
        return  JsonUtils.toList(JsonUtils.toJSONString(object),clazz);
    }
    public static Map stringToCollect(String s) {
        Map m = JSONObject.parseObject(s);
        return m;
    }
    public static String collectToString(Map m) {
        String s = JSONObject.toJSONString(m);
        return s;
    }


    /**
     * 实体转化为Map对象用于转化
     *
     * @param object 对象
     * @return {@link Map< String, Object>}
     */
    public static Map<String,Object> beanToMap(Object object){
        return JSONObject.parseObject(JsonUtils.toJSONString(object),new TypeReference<Map<String,Object>>(){});
    }

}